import { cateAdd, cateList, cateDel, cateOne,cateUpdate } from "../../request/api"
import { successAlert } from "../../utils/alert"
const state = {
    cate: [],
    cateOne:{}
}
const mutations = {
    cateLists(state, res) { 
        state.cate = res
    },
    cateOneChange(state,res){
        state.cateOne=res;
    },
    cateUpdateChange(state,res){
        state.cate=res;
    }

}
const actions = {
    cateAddActions(context, data) {
        cateAdd(data).then(res => {
            if (res.code === 200) {
                successAlert(res.msg);
            }
        })
    },
    cateListActions(context) {
        cateList({ istree: true }).then(res => {
            if (res.code === 200) {
                context.commit("cateLists", res.list);
            }
        })
    },
    cateDelActions(context, id) {
        cateDel(id).then(res => {
            if (res.code === 200) {
                successAlert(res.msg);
            }
        })
    },
    cateOneActions(context, id) {
        let params={
            id:id
        }
        cateOne(params).then(res => {
            if(res.code===200){
                res.list.id=id;
                context.commit("cateOneChange",res.list);
            }
        })
    },
    cateUpdateActions(cotext,data){
        console.log(data);
        cateUpdate(data).then(res=>{    
            console.log(res);
            if(res.code===200){
                successAlert(res.msg);
            }
        })
    }
}
const getters = {
    cate(state) {
        return state.cate;
    },
    cateOne(state){
        return state.cateOne;
    }
}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}